package com.example.coding_problem.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.coding_problem.Life_assistant_SQL;
import com.example.coding_problem.Life_assistant_list;
import com.example.coding_problem.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Life_LineChar3 extends Fragment {
    private Life_assistant_SQL dbHelper;
    private LineChart life_linechart4;
    private int max;
    private int min;
    int a=0;
    private ArrayList<Entry> pointValues4=new ArrayList<>();
    private List<Life_assistant_list> life_linechart_list4=new ArrayList<>();
    String[] name3={"","03", "06", "09", "12", "15", "18", "21","24", "27", "30","33","36","39","42","45","48","51","54","57","60"};
    public Life_LineChar3() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_life__line_char3, container, false);
        initView(view);
        initChart();
        initSql2();
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable,3000);
        return view;
    }
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable(){
        @Override
        public void run() {
            life_linechart_list4 = new ArrayList<>();
            initSql2();
            life_linechart4.invalidate();
            handler.postDelayed(this,3000);
        }
    };
    private void initSql2() {
        SQLiteDatabase db= dbHelper.getWritableDatabase();
        Cursor info2=db.query("Book",null,null,null,null,null,null);
        int i=0;
        life_linechart_list4= new ArrayList<>();
        while (info2.moveToNext()){
            Life_assistant_list data=new Life_assistant_list();
            i++;
            data.setCo2(Integer.parseInt(info2.getString(info2.getColumnIndex("cq2"))));
            life_linechart_list4.add(data);
            pointValues4=new ArrayList<>();
            for (int j=0;j<life_linechart_list4.size();j++){
                pointValues4.add(new Entry(j+1,life_linechart_list4.get(j).getCo2()));
                min=life_linechart_list4.get(0).getCo2();
                max=life_linechart_list4.get(0).getCo2();
                for (int a=0;a<pointValues4.size();a++){
                    if (min>life_linechart_list4.get(a).getCo2()){
                        min=life_linechart_list4.get(a).getCo2();
                    }
                    if (max<life_linechart_list4.get(a).getCo2()){
                        max=life_linechart_list4.get(a).getCo2();
                    }
                }
            }
            LineDataSet lds2=new LineDataSet(pointValues4,"过去一分钟二氧化碳最大浓度为："+max);
            lds2.setValueTextSize(10);
            LineData lds3=new LineData(lds2);
            life_linechart4.setData(lds3);
            life_linechart4.invalidate();
        }
    }
    private void initView(View view) {
        dbHelper = new Life_assistant_SQL(getActivity(),"BookStore.db",null,2);
        life_linechart4 = (LineChart) view.findViewById(R.id.life_linechart4);
        pointValues4=new ArrayList<>();
    }
    private void initChart() {
        life_linechart4.animateXY(1000, 1000);
        //=========================设置图例=========================
        // 像"□ xxx"就是图例
        Legend legend = life_linechart4.getLegend();
        //设置图例显示在chart那个位置 setPosition建议放弃使用了
        //设置垂直方向上还是下或中
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        //设置水平方向是左边还是右边或中
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        //设置所有图例位置排序方向
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //设置图例的形状 有圆形、正方形、线
        legend.setForm(Legend.LegendForm.CIRCLE);
        //是否支持自动换行 目前只支持BelowChartLeft, BelowChartRight, BelowChartCenter
        legend.setWordWrapEnabled(true);
        //=======================设置X轴显示效果==================
        XAxis xAxis = life_linechart4.getXAxis();
        //是否启用X轴
        xAxis.setEnabled(true);
        //是否绘制X轴线
        xAxis.setDrawAxisLine(true);
        //设置X轴上每个竖线是否显示
        xAxis.setDrawGridLines(false);
        //设置是否绘制X轴上的对应值(标签)
        xAxis.setDrawLabels(true);
        //设置X轴显示位置
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //设置竖线为虚线样式
        // xAxis.enableGridDashedLine(10f, 10f, 0f);
        //设置x轴标签数
        xAxis.setLabelCount(20, true);
        //图表第一个和最后一个label数据不超出左边和右边的Y轴
        // xAxis.setAvoidFirstLastClipping(true);
        //设置限制线 12代表某个该轴某个值，也就是要画到该轴某个值上
        LimitLine limitLine = new LimitLine(12);
        //设置限制线的宽
        limitLine.setLineWidth(1f);
        //设置限制线的颜色
        limitLine.setLineColor(Color.RED);
        xAxis.setGranularity(0);     //这个很重要
//        //自定义x轴
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return name3[(int) value];
            }
        });
        limitLine.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_TOP);
        limitLine.setLabel("马丹我是基线，也可以叫我水位线");
        //设置限制线为虚线
        limitLine.enableDashedLine(10f, 10f, 0f);
        //=================设置左边Y轴===============
        YAxis axisLeft = life_linechart4.getAxisLeft();
        //是否启用左边Y轴
        axisLeft.setEnabled(true);
        //设置最小值（这里就按demo里固死的写）
        axisLeft.setAxisMinimum(1000);
        //设置最大值（这里就按demo里固死的写了）
        axisLeft.setAxisMaximum(10000);
        //设置横向的线为虚线
        axisLeft.enableGridDashedLine(10f, 10f, 0f);
        //axisLeft.setDrawLimitLinesBehindData(true);
        //====================设置右边的Y轴===============
        YAxis axisRight = life_linechart4.getAxisRight();
        //是否启用右边Y轴
        axisRight.setEnabled(false);
        //设置最小值（这里按demo里的数据固死写了）
        axisRight.setAxisMinimum(0);
        //设置最大值（这里按demo里的数据固死写了）
        axisRight.setAxisMaximum(210);
        //设置横向的线为虚线
        axisRight.enableGridDashedLine(10f, 10f, 0f);
    }
}
